const { globalShortcut } = require("electron");
const path = require("path");
const store = require(path.join(__dirname, "../../store/store"));
const indexWin = require("../index/index");
const setVoice = (type, keyBoard, fn) => {
  const { key } = keyBoard;
  if (key === store.get(type)) return true;
  store.has(type) &&
    store.get(type) &&
    globalShortcut.unregister(`CommandOrControl+Alt+${store.get(type)}`);
  if (!key) {
    store.set(type, null);
    return true;
  }

  const res = globalShortcut.register(`CommandOrControl + Alt + ${key}`, fn);
  if (res) {
    store.set(type, key);
    return true;
  } else {
    return false;
  }
};
module.exports.setShoutCut = (event, type, keyBoard) =>
  setVoice(type, keyBoard, () => {
    type === "voice" && indexWin.getWindow().webContents.send("voice-swich");
    type === "mic" && indexWin.getWindow().webContents.send("mic-swich");
  });
